package com.boot.security.server.dao.mffl;

import java.util.List;
import java.util.Map;

import com.boot.security.server.model.mffl.MfflLawyerletter;
import com.boot.security.server.model.mffl.Vo.MfflConsultationTimerVo;
import com.boot.security.server.model.mffl.Vo.MfflConsultationVo;
import com.boot.security.server.model.mffl.Vo.MfflInformationVo;
import com.boot.security.server.model.mffl.mfflUserSimple;
import org.apache.ibatis.annotations.*;

import com.boot.security.server.model.mffl.MfflConsultation;

/**
 * 咨询dao
 */
@Mapper
public interface MfflConsultationDao {

    @Select("select * from mffl_consultation t where t.delete_state = 1 AND t.pay_merchant_number = #{paymerchantnumber}")
    MfflConsultation getBypaymerchantnumber(String paymerchantnumber);

    @Select("select * from mffl_consultation t where t.id = #{id}")
    MfflConsultation getById(Long id);

    @Delete("update mffl_consultation set delete_state = 0,deleteTime = now() where id = #{id}")
    int delete(Long id);

    int update(MfflConsultation mfflConsultation);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into mffl_consultation(userid, service_id, title, image_url, Defendant_attitude, my_attitude, Deadline, money, lawyer_id, trading_state, free_or_paid, free_or_paid_name, pay_id, pay_number, pay_merchant_number, best_answer_id, hot, type, like_number, collect_number, createTime, updateTime, delete_state, deleteTime, content, Defendant_url, My_appeal,replyNumber,state,myappealurl,rewarddays) values(#{userid}, #{serviceId}, #{title}, #{imageUrl}, #{DefendantAttitude}, #{myAttitude}, #{Deadline}, #{money}, #{lawyerId}, #{tradingState}, #{freeOrPaid}, #{freeOrPaidName}, #{payId}, #{payNumber}, #{payMerchantNumber}, #{bestAnswerId}, #{hot}, #{type}, #{likeNumber}, #{collectNumber}, #{createTime}, #{updateTime}, #{deleteState}, #{deleteTime}, #{content}, #{DefendantUrl}, #{MyAppeal},#{replyNumber},#{state},#{myappealurl},#{rewarddays})")
    int save(MfflConsultation mfflConsultation);

    int count(@Param("params") Map<String, Object> params);

    List<MfflConsultation> list(@Param("params") Map<String, Object> params, @Param("offset") Integer offset, @Param("limit") Integer limit);

    //获取参与过的咨询
    int participatecount(@Param("params") Map<String, Object> params);

    List<MfflConsultation> participatecountlist(@Param("params") Map<String, Object> params, @Param("offset") Integer offset, @Param("limit") Integer limit);
    //end获取参与过的咨询

    /**
     * 查看用户下的问题是否存在。
     */
    @Select("SELECT * FROM `mffl_consultation` WHERE userid = #{map.userid} AND id = #{map.id} AND delete_state = 1")
    MfflConsultation getUserConsultationid(@Param("map") Map<String,Long> map);

    /**
     * 查看逾期问题
     */
    @Select("SELECT * FROM `mffl_consultation` WHERE free_or_paid = 1 AND best_answer_id = 0 AND delete_state = 1 AND Deadline < NOW() AND state = 1")
    List<MfflConsultation> getBeoverdue();

    //律师端首页推荐案例
    @Select("select mc.* ,su.nickname,su.headImgUrl from mffl_consultation  mc LEFT JOIN sys_user su ON su.id = mc.userid where delete_state = 1 AND type = #{type} and mc.state = 1  order by mc.createTime desc,mc.money desc,mc.hot desc limit #{count}")
    List<MfflConsultationVo> getConsultationByType(@Param("type") int type, @Param("count") Integer count);

    @Select("select mc.* ,su.nickname,su.headImgUrl from mffl_consultation  mc LEFT JOIN sys_user su ON su.id = mc.userid where mc.state = 1  order by mc.createTime desc,mc.money desc,mc.hot desc limit #{count}")
    List<MfflConsultationVo> getConsultationByTypeRecommend(@Param("count") Integer count);

    int getWorkbenchCount(@Param("params") Map<String, Object> params);

    List<MfflConsultationVo> getWorkbenchList(@Param("params") Map<String, Object> params, @Param("offset") Integer offset, @Param("limit") Integer limit);

    //后台免费咨询管理
    int freeConsultationsCount(@Param("params") Map<String, Object> params);
    List<MfflConsultationVo> freeConsultationsList(@Param("params") Map<String, Object> params, @Param("offset") Integer offset, @Param("limit") Integer limit);

    @Update("update mffl_lawyerletter set payState = #{payState} where payMerchantNumber = #{payMerchantNumber} ")
    void udpateState(@Param("payMerchantNumber") String payMerchantNumber,@Param("payState") int payState);


    /**
     * 定时任务
     * 获取满足条件的数据（具体配置在mffl_service服务表中配置）
     *      1.用户发起咨询，没有律师回答
     *      2.用户发起咨询，有人回答但是用户没有设为最佳答案
     */
    @Select("SELECT a.id,a.replyNumber,a.pay_merchant_number,a.service_id,a.userid FROM mffl_order AS b,mffl_consultation AS a \n" +
            "WHERE	a.delete_state = 1 	AND a.pay_merchant_number = b.pay_merchant_number 	AND a.trading_state = 0 AND a.free_or_paid = 1 AND a.best_answer_id = 0 AND a.state = 1 AND (( UNIX_TIMESTAMP()- UNIX_TIMESTAMP( a.Deadline ))* 1000 ) > #{ConfirmTime} FOR UPDATE")
    List<MfflConsultation> getTimerTaskData(@Param("ConfirmTime") Long ConfirmTime, @Param("RefuseTime") Long RefuseTime);

    //修改回复数量
    @Update("update mffl_consultation set replyNumber = replyNumber-1 where id = #{consultationId} ")
    void updateReplyNumber(@Param("consultationId") Integer consultationId);
}
